package com.skcc.cop.mrtaxi.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.skcc.cop.mrtaxi.BaseActivity;
import com.skcc.cop.mrtaxi.MrTaxiApplication;
import com.skcc.cop.mrtaxi.R;
import com.skcc.cop.mrtaxi.http.model.Driver;


public class Dialogs {
	
	private static AlertDialog dialog;
	public interface DialogListener {
		
		public void onClickedOk();
		public void onClickedCancel();
	}
	
	
	public static void showDialogYesNo(Context context, String message, final DialogListener listener) {
    	
    	LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	View customTitleView = inflater.inflate(R.layout.view_customtitle, null, false);
    	
    	dialog = new AlertDialog.Builder(context)
				.setCustomTitle(customTitleView)
				.setMessage(message)
				.setCancelable(false)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						listener.onClickedOk();
					}
				}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						listener.onClickedCancel();
					}
				}).show();
	}
	
	
	public static void showDialog(Context context, String message, final DialogListener listener) {
    	
    	LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	View customTitleView = inflater.inflate(R.layout.view_customtitle, null, false);
    	
    	dialog = new AlertDialog.Builder(context)
				.setCustomTitle(customTitleView)
				.setMessage(message)
				.setCancelable(false)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						listener.onClickedOk();
					}
				}).show();
	}
	
	
	public static void showDialogDriverInfo(Context context, Driver driver, final DialogListener listener) {
		MrTaxiApplication application = (MrTaxiApplication) ((BaseActivity) context).getApplication();
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	
    	View customTitleView = inflater.inflate(R.layout.dialog_driver_title, null, false);
    	View customDialogView = inflater.inflate(R.layout.dialog_driver, null, false);
    	ImageView imgUrlImageview = (ImageView) customDialogView.findViewById(R.id.driver_imageurl_imageview);
    	TextView driverNameTextview = (TextView) customDialogView.findViewById(R.id.driver_name_textview);
    	TextView carNumberTextview = (TextView) customDialogView.findViewById(R.id.driver_carnumber_textview);
    	TextView companyNameTextview = (TextView) customDialogView.findViewById(R.id.driver_companyname_textview);
    	
    	String imgUrl = driver.getImgUrl();
    	if ( "".equals(imgUrl) ) {
    		imgUrlImageview.setBackgroundResource(R.drawable.not_registered);
    	} else {
    		application.getImageLoader().displayImage(imgUrl, imgUrlImageview);
    	}
    	driverNameTextview.setText("기사 이름 : " + driver.getName());
    	carNumberTextview.setText("차량 번호 : " + driver.getCarNumber());
    	companyNameTextview.setText("회사명 : " + 	driver.getCompany());
    	
    	dialog = new AlertDialog.Builder(context)
    			.setCustomTitle(customTitleView)
    			.setView(customDialogView)
    			.setPositiveButton("요청", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						listener.onClickedOk();
					}
				})
		        .setNegativeButton("취소", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						listener.onClickedCancel();
					}
				}).show();
	}

	
	public static void dismiss() {
		if (dialog != null) {
			dialog.dismiss();
		}
	}
	
}
